﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using StoryPlan.Models;

namespace StoryPlan.Api
{
    public class CharacterCategoryController : ApiControllerBase
    {
        private MainDataContext db = new MainDataContext();

        // GET api/CharacterCategory
        public IEnumerable<CharacterCategory> GetCharacterCategories()
        {
            var userId = GetUserId();
            return db.CharacterCategories.Where(cc => cc.UserId == userId).AsEnumerable();
        }

        // GET api/CharacterCategory/5
        public CharacterCategory GetCharacterCategory(int id)
        {
            CharacterCategory charactercategory = db.CharacterCategories.Find(id);
            if (charactercategory == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return charactercategory;
        }

        // PUT api/CharacterCategory/5
        public HttpResponseMessage PutCharacterCategory(int id, CharacterCategory charactercategory)
        {
            if (ModelState.IsValid && id == charactercategory.Id)
            {
                db.Entry(charactercategory).State = EntityState.Modified;

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // POST api/CharacterCategory
        public HttpResponseMessage PostCharacterCategory(CharacterCategory charactercategory)
        {
            if (ModelState.IsValid)
            {
                db.CharacterCategories.Add(charactercategory);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, charactercategory);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = charactercategory.Id }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // DELETE api/CharacterCategory/5
        public HttpResponseMessage DeleteCharacterCategory(int id)
        {
            CharacterCategory charactercategory = db.CharacterCategories.Find(id);
            if (charactercategory == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.CharacterCategories.Remove(charactercategory);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return Request.CreateResponse(HttpStatusCode.OK, charactercategory);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}